package com.marvels.system.permission.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.system.permission.dao.McsmUserLoginLogMapper;
import com.marvels.system.permission.dto.McsmUserLoginLog;
import com.marvels.system.permission.service.McsmUserLoginLogService;

import cn.hutool.core.util.StrUtil;

/**
 * 用户登陆日志信息SERVICE
 * 
 * @title McsmUserLoginLogServiceImpl
 * @author tuxu
 * @date 2018年10月3日
 */
@Service("mcsmUserLoginLogService")
public class McsmUserLoginLogServiceImpl implements McsmUserLoginLogService {

	@Autowired
	private McsmUserLoginLogMapper mcsmUserLoginLogMapper;

	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Override
	public Page<McsmUserLoginLog> getLoginLog(McsmUserLoginLog conditions, Integer pageNo, Integer pageSize)
			throws MarvelsBusinessException {
		if (pageNo <= 0) {
			pageNo = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		if (StringUtils.isNotEmpty(conditions.getSortBy())) {
			PageHelper.orderBy(conditions.getSortBy() + " " + conditions.getSortType());
		}
		if (StringUtils.isNotBlank(conditions.getLoginStartDate()) && conditions.getLoginStartDate().length()>10) {
			conditions.setLoginStartDate(conditions.getLoginStartDate().substring(0, 10));
		}
		if (StringUtils.isNotBlank(conditions.getLoginEndDate()) && conditions.getLoginEndDate().length()>10) {
			conditions.setLoginEndDate(conditions.getLoginEndDate().substring(0, 10));
		}
		
		String ignoreUserCodes= ServerProperties.getValue(configPropertiesPath, "common.properties", "ignore_user_code");
		if(StrUtil.isNotEmpty(ignoreUserCodes)){
			conditions.setIgnoreUserCodeList(Arrays.asList(ignoreUserCodes.split(",")));
		}
		PageHelper.startPage(pageNo, pageSize);
		MarvelsLogUtil.getInstance().info(pageNo + pageSize + "分页查询conditions" + conditions);
		Page<McsmUserLoginLog> userPage = mcsmUserLoginLogMapper.getLoginLog(conditions);
		return userPage;
	}
}
